package utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件读写工具类
 * @author hulianfu
 *
 */
public class FileUtils {
	
	
	private static final int BUFFER_SIZE = 16 * 1024;
	
	public static boolean copy(File src, File dst){
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			return true;
		} catch (Exception e) {
			return false;
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
					//LogUtils.logError(FileShareAction.class, e);
				}
			}
		}
	}
	
	/**
	 * 按指定编码写入文件
	 * @param filePath	文件全路径
	 * @param content	写入内容
	 * @param charsetName	字符编码
	 * @return true:保存成功，false保存失败
	 */
	public static boolean writeFile(String filePath, String content, String charsetName){
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath);
			OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);
			osw.write(content);
			osw.flush();
			return true;
		} catch (Exception e) {
			return false;
		}finally{
			if(null != fos){
				try {
					fos.close();
				} catch (IOException e) {
				}
			}
		}
	}
	/**
	 * 按指定编码读取文件
	 * @param filePath	文件全路径
	 * @param charseName	字符编码
	 * @return	文件内容,null读文件异常
	 */
	public static String readFile(String filePath, String charseName){
		FileInputStream fis = null;
		StringBuilder result = new StringBuilder();
		try {
			fis = new FileInputStream(filePath);
			InputStreamReader isr = new InputStreamReader(fis, charseName);
			BufferedReader bReader = new BufferedReader(isr);
			String s = null;
			while ((s=bReader.readLine())!=null) {
				result.append(s);
			}
		} catch (Exception e) {
			return null;
		}finally{
			try {
				if(null != fis)
					fis.close();
			} catch (IOException e) {
			}
		}
		return result.toString();
	}
	
	/**
	 * 创建目录
	 * @param dirPath	目录全路径
	 */
	public static void makeDir(String dirPath){
		File dir = new File(dirPath);
		if(!dir.exists()){
			dir.mkdir();
		}
	}
	
	/**
	 * 获取目录下的文件列表
	 * @param dirPath	目录路径
	 * @return	文件列表
	 */
	public static List<File> getFileList(String dirPath){
		File dir = new File(dirPath);
		List<File> results = new ArrayList<File>();
		if(dir.exists()){
			File[] files = dir.listFiles();
			for (File file : files) {
				if(file.isFile()){
					results.add(file);
				}
			}
		}
		return results;
	}
	/**
	 * 删除指定的文件
	 * @param filePath	文件路径
	 * @return
	 */
	public static boolean deleteFile(String filePath){
		File file = new File(filePath);
		boolean result = false;
		if(file.exists()){
			result = file.delete();
		}
		return result;
	}
}
